<?php
/*
	
*/
defined('BY_JYA') or exit('Access Denied');
global $_RQ,$_AR;

$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return['diy']=$this->plugin_setting('diy');
if($_RQ['op']=='main'){
	exi($return);
}
/*******op*******/
if($_RQ['op']=='list'){
	$worker=pdo_get('jy_qiuyuyue_store_worker',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
	$params=array('worker_id'=>$worker['id'],'uniacid'=>$_SESSION['uniacid']);
	if(isset($_RQ['status'])){
		$params['status']=intval($_RQ['status']);
		$return['status']=$_RQ['status'];
	}else{
		$params['status']=1;
		$return['status']=1;
	}
	if($_RQ['today']){
		$params['createtime >']=strtotime(date('ymd'));
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$list=pdo_getslice('jy_qiuyuyue_server_order',$params,array($pindex,$psize),$total,$fields,'','createtime desc');
	if($list){
		foreach($list as &$item){
			$member=model('core/member')->detail($item['mid'],array('nickname','mobile'));
			$serve=pdo_get('jy_qiuyuyue_serve',array('id'=>$item['serve_id'],'uniacid'=>$_SESSION['uniacid']));
			$item['user_name']=$member['nickname'];
			$item['user_mobile']=$member['mobile'];
			$item['serve_title']=$serve['title'];
			$item['serve_image']=$serve['image'];
			$item['package']=json_decode($item['package']);
			$item['createtime']=date('Y-m-d H:i:s', $item['createtime']);
			$item['serve_typetitle']=$this->serve_types[$item['serve_type']];
			$serve_times=json_decode($serve['serve_time'],true);
			$item['serve_time']=$serve_times[$item['timeid']];			
		}
	}
	$return['list']=$list;
	exi($return);
}

//订单详情
if($_RQ['op']=='detail'){
	if(!$_RQ['id']){
	exi('订单不存在，请重新选择','error');
	}
	$order=pdo_get('jy_qiuyuyue_server_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	//用户信息
	$member=pdo_get('core_members',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['mid']));
	$user=pdo_get('jy_qiuyuyue_member',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['mid']));
	//项目信息
	$store=pdo_get('jy_qiuyuyue_store',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['store_id']));	
	$store_times=json_decode($store['serve_time'],true);
	$order['serve_time']=$store_times[$order['timeid']];
	//自定义字段数据
	$return['order']=$order;
	$return['user']=$user;
	$return['member']=$member;
	$return['serve']=$store;
	exi($return);
}
//员工确认订单
if($_RQ['op']=='receive'){
	if(!$_RQ['id']){
		exi('订单不存在，请重新选择订单','error');
	}
	$order=pdo_get('jy_qiuyuyue_server_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	$user=pdo_get('jy_qiuyuyue_member',array('id'=>$order['mid'],'uniacid'=>$_SESSION['uniacid']));
	$store=pdo_get('jy_qiuyuyue_store',array('id'=>$order['store_id'],'uniacid'=>$_SESSION['uniacid']));
	$store_times=pdo_getcolumn('jy_qiuyuyue_store',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['store_id']),'time');
	$store_times=json_decode($store_times,true);
	$store_time=$store_times[$order['timeid']];
	$order['createtime']=date("Y-m-d h:i:s", $order['createtime']);
	$postdata = array(
					'first' => array(
						'value' => '您的'.$store['title'].'项目，技师已接单！',
						'color' => '#990000'
					),
					'keyword1' => array(
						'value' => $order['day'].'--'.$store_time['times']['start'].'-'.$store_time['times']['end'],
						'color' => '#ff510'
					),
					'keyword2' => array(
						'value' => $store['title'],
						'color' => '#ff510'
					),
					'keyword3' => array(
						'value' => $order['username'],
						'color' => '#ff510'
					),
					'keyword4' => array(
						'value' => $order['createtime'],
						'color' => '#ff510'
					),
					'remark' => array(
						'value' => '点击查看详细信息',
						'color' => '#ff510'
					),							
				);
	$tpl=$this->plugin_setting('message_tpl');
	$url=custom_url('jy_qiuyuyue/server_order/',true);
	if($tpl['confirm']){
		cfc('account')->sendTplNotice($order['mid'],$tpl['confirm'],$postdata,$url,'#980000');
	}	
	pdo_update('jy_qiuyuyue_server_order',array('status'=>2),array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	exi('接收服务任务成功');
}
//员工完成
if($_RQ['op']=='complete'){
	if(!$_RQ['id']){
		exi('订单不存在，请重新选择订单','error');
	}
	$order=pdo_get('jy_qiuyuyue_server_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	$user=pdo_get('jy_qiuyuyue_member',array('id'=>$order['mid'],'uniacid'=>$_SESSION['uniacid']));
	$store=pdo_get('jy_qiuyuyue_store',array('id'=>$order['store_id'],'uniacid'=>$_SESSION['uniacid']));
	$store_times=pdo_getcolumn('jy_qiuyuyue_store',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['store_id']),'time');
	$store_times=json_decode($store_times,true);
	$store_time=$store_times[$order['timeid']];
	$order['createtime']=date("Y-m-d h:i:s", $order['createtime']);
	$postdata = array(
					'first' => array(
						'value' => '您的'.$store['title'].'项目，技师已完成，感谢您的使用！',
						'color' => '#990000'
					),
					'keyword1' => array(
						'value' => $order['day'].'--'.$store_time['times']['start'].'-'.$store_time['times']['end'],
						'color' => '#ff510'
					),
					'keyword2' => array(
						'value' => $store['title'],
						'color' => '#ff510'
					),
					'keyword3' => array(
						'value' => $order['username'],
						'color' => '#ff510'
					),
					'keyword4' => array(
						'value' => $order['createtime'],
						'color' => '#ff510'
					),
					'remark' => array(
						'value' => '点击查看详细信息',
						'color' => '#ff510'
					),							
				);
	$tpl=$this->plugin_setting('message_tpl');
	$url=custom_url('jy_qiuyuyue/server_order/',true);
	if($tpl['confirm']){
		cfc('account')->sendTplNotice($order['mid'],$tpl['confirm'],$postdata,$url,'#980000');
	}	
	pdo_update('jy_qiuyuyue_server_order',array('status'=>3),array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	exi('服务任务成功');
}
//员工取消订单
if($_RQ['op']=='unreceive'){
	if(!$_RQ['id']){
		exi('订单不存在，请重新选择订单','error');
	}
	pdo_update('jy_qiuyuyue_server_order',array('status'=>'3'),array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	exi('取消订单成功');
}